package com.dao;

import com.utils.DbUntil;
import com.model.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/28.
 */
public class UserDao {

    private Connection connection;

    //在这里初始化  这里获取链接
    public UserDao() {
        connection = DbUntil.getConnection();
    }

    //公有方法 获取所有用户信息
    public List<User> getAllUsers() {
        List<User> users = new ArrayList<User>();
        try {
            Statement statement = connection.createStatement();
            //这里是查询select * from blogs
            ResultSet rs = statement.executeQuery("select * from users");
            while (rs.next()) {
                User user = new User();
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return users;
    }

    // 登陆方法
    public User login(String name , String pwd) {
        List<User> users = new ArrayList<User>();
        try {
            Statement statement = connection.createStatement();
            String sql = " select * from users where user_name = '"+name+"' and  password = '"+pwd+"' ";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                    User user = new User();
                    user.setId(rs.getString("user_id"));
                    user.setOpenId(rs.getString("openId"));
                    user.setUserName(rs.getString("user_name"));
                    user.setPhoto_address(rs.getString("photo_address"));
                    user.setphoneNumber(rs.getString("phone_number"));
                    user.setSex(rs.getString("sex"));
                    user.setStatus(rs.getString("status"));
                    user.setCreationDate(rs.getLong("creationDate"));
                    users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(users.size()>0){
            return users.get(0);
        }
        else{
            return null;
        }
    }

    //QQ登陆方法
    public User loginByQQ(String openId){
        List<User> users = new ArrayList<User>();
        try {
            Statement statement = connection.createStatement();
            String sql = " select * from users where openId = '"+openId+"'";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getString("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setphoneNumber(rs.getString("phone_number"));
                user.setPhoto_address(rs.getString("photo_address"));
                user.setSex(rs.getString("sex"));
                user.setStatus(rs.getString("status"));
                user.setCreationDate(rs.getLong("creationDate"));
                user.setOpenId(rs.getString("openId"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(users.size()>0){
            return users.get(0);
        }
        else{
            return null;
        }
    }

    //注册方法
    public int register (String number,String name , String pwd){

        int boo = 0;
        int userNumber = 0;
        long data = new Date().getTime();
        //String photo_address ="";
        if(this.login(name,pwd)==null){
            try {
                    Statement statement = connection.createStatement();
                    String sql1="select count(user_id) from users";
                    ResultSet rs = statement.executeQuery(sql1);
                    while (rs.next()) {
                        userNumber = rs.getInt("count(user_id)");
                        userNumber++;
                    }
                    String sql2=" INSERT INTO `users` (`user_id`, `user_name`, `password`, `phone_number`,`creationDate`) VALUES ('"+userNumber+"', '"+name+"', '"+pwd+"', '"+number+"', '"+data+"') ";

                    //这里是查询select * from blogs
                    boo = statement.executeUpdate(sql2);
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            return boo;
        }
        else{
            return 0;
        }
    }

    //QQ注册方法
    public int registerByQQ (String number,String name , String pwd,String openId){
        int boo = 0;
        int userNumber = 0;
        long data = new Date().getTime();
        if(this.login(name,pwd)==null){
            try {
                Statement statement = connection.createStatement();
                String sql1="select count(user_id) from users";
                ResultSet rs = statement.executeQuery(sql1);
                while (rs.next()) {
                    userNumber = rs.getInt("count(user_id)");
                    userNumber++;
                }
                String sql2=" INSERT INTO `users` (`user_id`, `user_name`, `password`, `phone_number`,`creationDate`,`openId`) VALUES ('"+userNumber+"', '"+name+"', '"+pwd+"', '"+number+"', '"+data+"','"+openId+"') ";

                //这里是查询select * from blogs
                boo = statement.executeUpdate(sql2);
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            return boo;
        }
        else{
            return 0;
        }
    }
}
